﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using DotNet.Business;
using DotNet.Model;
using DotNet.Common;
using System.Text;
using System.Collections.Generic;

public partial class Admin_adminNew : basePage
{
    NewsBll newsBLL = new NewsBll();
    newClass newsClass = new newClass();
    newClassBll nCBLL = new newClassBll();    
    int pagesize =20;
    public int num = 0;
    public string input = "<input id='HiddenUrl' type='hidden' value='&CurrentIndex=1' />";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request.QueryString["action"]!=null)
            {
               
                string action = Request.QueryString["action"];
                string id = Request["id"].ToString();
                switch(action.Trim())
                {
                    case "delAll":
                        delAll(id);                       
                        break;
                    case"MoveAll":
                        moveUpdate(Convert.ToInt32(id));
                        break;
                    default:
                        break;
                }
            }
            ViewState["dt"] = nCBLL.GetModel();
            bindNews();
        }        
      
    }
    #region 绑定新闻---郑同飞
    /// <summary>
    /// 绑定新闻---郑同飞
    /// </summary>
    public void bindNews()
    {
        if (Request.QueryString["CurrentIndex"]!=null)
        {
            _currentIndex = Convert.ToInt32(Request.QueryString["CurrentIndex"]);           
        }
        if (Request.QueryString["ClassId"]!=null)
        {
            _classid = Convert.ToInt32(Request.QueryString["ClassId"]);
        }

        if (Request.QueryString["Key"]!=null)
        {
            _key = Request.QueryString["Key"].ToString();
        }
        
        DataTable dt = newsBLL.pageingForNews(_currentIndex, pagesize, _classid, _key);
        if (dt.Rows.Count==0&&_currentIndex>1)
        {
            --_currentIndex;
            dt = newsBLL.pageingForNews(_currentIndex,pagesize,_classid,_key);
        }
        input = "<input id='HiddenUrl' type='hidden' value='&CurrentIndex=" + _currentIndex + "&ClassId=" + _classid + "&Key=" + _key + "' />";
        this.Repeater1.DataSource = dt;
        this.Repeater1.DataBind();
    }
    #endregion

    #region 状态维护属性
    private int _currentIndex = 1;
    private int _classid = 0;
    private string _key = "";
    #endregion

    #region 分页按钮---郑同飞
    /// <summary>
    /// 分页按钮---郑同飞
    /// </summary>
    public void htmlPaging() 
    {
        string str=null;
        int count=0;
         count = newsBLL.getRecordCount(_classid, _key);
         int pcount = count % pagesize == 0 ? count / pagesize : count / pagesize + 1;
         str = "页次：" + _currentIndex + "/" + pcount + "  每页" + pagesize + " 总收录数据" + count + "条";
         str += HtmlControls.pagIng(_currentIndex, pcount, "adminNew.aspx?ClassId=" + _classid + "&key=" +Server.UrlEncode(_key) + "&CurrentIndex", "pageaa", "pagecurrent");      
        Response.Write(str);
    }
    #endregion

    #region 自定义下拉框--郑同飞   
    /// <summary>
    /// 自定义下拉框---郑同飞
    /// </summary>
    /// <param name="name">控件的id</param>
    /// <returns></returns>
    public string NewsClassDp(string name)
    {
        StringBuilder str = new StringBuilder();
        IList<newClass>list = ViewState["dt"] as IList<newClass>;
        str.Append("<select name='selectDp' id='" + name + "' style='width:100px;'>");
        str.Append("<option value='0'>所有分类</option>");
        foreach (newClass nc in list)
        {
            str.Append("<option value='" + nc.newClassId + "'>" + nc.newClassName + "</option>");

        }
        str.Append("</select>");
        return str.ToString();
    }
    #endregion

    #region ajax批量删除，sql拼接需要过滤的地方---郑同飞
    /// <summary>
    /// ajax批量删除，sql拼接需要过滤的地方---郑同飞
    /// </summary>
    /// <param name="id"></param>
    public void delAll(string id) 
    {
        if (!id.Contains(","))
        {
            newsBLL.Delete(Convert.ToInt32(id));
        }
        else 
        {
            string[] idParam = id.Split(',');
            string[] nid = new string[idParam.Length];
            for (int i = 0; i < idParam.Length; i++)
            {
                nid[i] = idParam[i];
            }
            newsBLL.Delete(nid);
        }
    }
    #endregion    

    #region 批量移动新闻---郑同飞
    /// <summary>
    /// 批量移动新闻---郑同飞
    /// </summary>
    /// <param name="classId"></param>
    public void moveUpdate(int classId)
    {
        try
        {
            string nid = Request.QueryString["pid"].ToString();
            if (nid.Contains(","))
            {
                string[] idParam = nid.Split(',');
                string[] nidParam = new string[idParam.Length];
                for (int i = 0; i <= nidParam.Length - 1; i++)
                {
                    nidParam[i] = idParam[i];
                }
                if (newsBLL.Update(nidParam,classId))
                {

                }
            }
            else
            {
                string[] newid ={ nid };
                if (newsBLL.Update(newid, classId))
                {


                }
            }
        }
        catch
        {

        }
    }
    #endregion

    #region 根据新闻类别id获得类别名称---郑同飞
    /// <summary>
    /// 根据新闻类别id获得类别名称---郑同飞
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    public string getNameByClassId(int id)
    {
        string str = null;
        newClass newClass = new newClass();
        if (id == 0)
        {
            str = "<font color='red'>类别未绑定</red>";
        }
        else
        {
            foreach (newClass nc in ViewState["dt"] as IList<newClass>)
            {
                if (nc.newClassId == id)
                {
                    str = nc.newClassName;
                    break;
                }
            }
        }
        return str;
    }
    #endregion



}
